@extends('frontend/layouts/bootstrap')

@section('content')
<h1>Машин бүртгэх</h1>

{{ Form::open(array('route' => 'car_balance.car.create', 'files' => true)) }}

<h4>Өмнөх алхамын сонгосон мэдээлэл</h4>
<div class="well">
    <b>Service company:</b>    {{ $service_company_list[$step1_data['service_company_id']] }},
    <b>Auction type:   </b>    {{ $car_auction_type_list[$step1_data['car_auction_type']] }},
    <b>Auction name:   </b>    {{ $car_auction_list[$step1_data['car_auction_id']] }},
    <b>Price set:       </b>    {{ $step1_data['customer_id'] ? $customer_list[$step1_data['customer_id']] : 'NULL' }},
    <b>Container port: </b>    {{ $step1_data['container_port'] ? $container_ports[$step1_data['container_port']] : 'NULL' }}
</div>


<h4>Үндсэн мэдээлэл</h4>
<table class="table table-bordered">
    <thead style="background-color: #f0f0f0;">
        <th title="Date">日付</th>
        <th title="Exhibit number">出品番号</th>
        <th title="Car model">車種</th>
        <th title="Model year">年式 / 年月</th>
        <th title="Chassis number">車体番号</th>
        <th title="Number plate flag">Number plate</th>
        <th title="Auction sheet">オークションシート</th>
    </thead>

    <tr>
        <td{{ $errors->first('purchase_date', ' class="control-group error"') }}>
            <input class="datepicker input-medium" placeholder="Purchase Date" title="Purchase Date" name="purchase_date" type="text" value="{{ Input::old('purchase_date') }}">
            {{ $errors->first('purchase_date', '<span class="help-inline">:message</span>') }}
        </td>
        <td{{ $errors->first('lot_no', ' class="control-group error"') }}>
            <input class="input-small" placeholder="Exhibit Number" title="Lot Number" name="lot_no" type="text" value="{{ Input::old('lot_no') }}">
            {{ $errors->first('lot_no', '<span class="help-inline">:message</span>') }}
        </td>
        <td{{ $errors->first('car_maker', ' class="control-group error"') }}>
            {{ Form::select('car_maker', $car_makers, '', array('class'=>'input-medium')) }}
            {{ $errors->first('car_maker', '<span class="help-inline">:message</span>') }}
            <div id="car_name"></div>
        </td>
        <td{{ $errors->first('car_year', ' class="control-group error"') }} {{ $errors->first('car_year_month', ' class="control-group error"') }}>
            <input class="input-small" placeholder="Model Year" title="Model Year" name="car_year" type="text" value="{{ Input::old('car_year') }}" data-provide="typeahead" data-source='[@for($i=1990;$i<2013;$i++)"{{$i}}",@endfor"2014"]'>
            <input class="input-small" placeholder="Year month" title="Year month" name="car_year_month" type="text" value="{{ Input::old('car_year_month') }}">
        {{ $errors->first('car_year', '<span class="help-inline">:message</span>') }} {{ $errors->first('car_year_month', '<span class="help-inline">:message</span>') }}
        </td>
        <td{{ $errors->first('chasis_no', ' class="control-group error"') }}>
            <input class="" placeholder="Chasis Number" title="Chasis Number" name="chasis_no" type="text" value="{{ Input::old('chasis_no') }}">
            {{ $errors->first('chasis_no', '<span class="help-inline">:message</span>') }}
        </td>
        <td{{ $errors->first('number_plate_flag', ' class="control-group error"') }}>
            <input id="number_plate_flag" placeholder="チェック" type="checkbox" class="input-small" style="margin-bottom: 5px; margin-right: 5px;" name="number_plate_flag" {{  Input::old('number_plate_flag') ? 'checked' : '' }}/>
        {{ $errors->first('number_plate_flag', '<span class="help-inline">:message</span>') }}
        </td>
        <td>
            <div class="fileupload fileupload-new" data-provides="fileupload">
                <div id="auction_sheet_upload" class="input-append">
                    <div class="uneditable-input span3">
                        <i class="icon-file fileupload-exists"></i>
                        <span class="fileupload-preview"></span>
                    </div>
                    <div>
                    <span class="btn btn-file">
                        <span class="fileupload-new">Select file</span>
                        <span class="fileupload-exists">Change</span>
                        <input name="auction_sheet_upload" type="file" />
                    </span></br>
                    <a href="#" class="btn fileupload-exists" data-dismiss="fileupload">Remove</a>
                    </div>
                </div>
            </div>
        </td>
    </tr>
</table>

<h4>Payments</h4>
<table class="table table-bordered">
    <thead style="background-color: #f0f0f0;">
        <th title="Purchase price">購入額</th>
        <th title="VAT (%)">消費税 (%)</th>
        <th title="Recycle Amount">リサイクル</th>
        <th title="Auction commission">落札料</th>
        <th title="Car tax amount">自動車税</th>
        <th title="Transport fee">Transport fee</th>
        <th title="Others">その他</th>
    </thead>

    <tr>
        <td>
            <input class="input-small" placeholder="Purchase Price" title="Purchase Price" name="purchase_price" type="text" id="purchase_price">
        </td>
        <td>
            <select name="vat_percent" id="vat_percent" class="input-small">
                <option value="8" {{ Input::old('vat_percent') == '8' ? 'selected' : '' }}>8 %</option>
                <option value="5" {{ Input::old('vat_percent') == '5' ? 'selected' : '' }}>5 %</option>
                <option value="3" {{ Input::old('vat_percent') == '3' ? 'selected' : '' }}>3 %</option>
                <option value="0" {{ Input::old('vat_percent') == '0' ? 'selected' : '' }}>0 %</option>
            </select>
        </td>
        @include('nimo.domains.car_balance.Main._form_field', array('field'=>'recycle_amount'))
        @include('nimo.domains.car_balance.Main._form_field', array('field'=>'auction_commission'))
        @include('nimo.domains.car_balance.Main._form_field', array('field'=>'car_tax_amount'))
        @include('nimo.domains.car_balance.Main._form_field', array('field'=>'transfer_fee', 'value' => $transport_fee))
        @include('nimo.domains.car_balance.Main._form_field', array('field'=>'others'))
    </tr>
</table>

<div style="@if ( Sentry::getUser()->hasAccess('operator')) display:none; @endif">
<h4>Bill</h4>
<ul class="nav nav-tabs" id="bill_info">
    <li class="{{ ( $step1_data['customer_id'] == 'BH' or $step1_data['customer_id'] == 'ME' ) ? 'active' : '' }}"><a href="#bill_type_1" data-toggle="tab">Bill type 1</a></li>
    <li class="{{ $step1_data['customer_id'] == 'CH' ? 'active' : '' }}"><a href="#bill_type_2" data-toggle="tab">Bill type 2</a></li>
    <li class="{{ $step1_data['customer_id'] == 'KG' ? 'active' : '' }}"><a href="#bill_type_3" data-toggle="tab">Bill type 3</a></li>
    <li class="{{ $step1_data['customer_id'] == 'NMC' ? 'active' : '' }}"><a href="#bill_type_4" data-toggle="tab">Bill type 4</a></li>
    <li class="{{ $step1_data['customer_id'] ? '' : 'active' }} {{ $step1_data['customer_id'] == 'BAY' ? 'active' : '' }}"><a href="#bill_type_0" data-toggle="tab">Bill type 0</a></li>
</ul>

<div class="tab-content">
    <div class="tab-pane {{ ( $step1_data['customer_id'] == 'BH' or $step1_data['customer_id'] == 'ME' ) ? 'active' : '' }}" id="bill_type_1">
        <table class="table table-bordered" id="billDetailed">
            <thead style="background-color: #f0f0f0;">
            <th title="Price">Price</th>
            <th title="VAT (%)">VAT (%)</th>
            <th title="Transport">Transport</th>
            <th title="Commission">Commission</th>
            </thead>
            <tr>
                <td>
                    <input class="input-small" placeholder="Bill Price" title="Bill Price" name="bill_price_1" type="text" id="bill_price_1">
                </td>
                <td>
                    <select name="bill_vat_1" id="bill_vat_1" class="input-small">
                        <option value="8" {{ Input::old('bill_vat') == '8' ? 'selected' : '' }}>8 %</option>
                        <option value="5" {{ Input::old('bill_vat') == '5' ? 'selected' : '' }}>5 %</option>
                        <option value="3" {{ Input::old('bill_vat') == '3' ? 'selected' : '' }}>3 %</option>
                        <option value="0" {{ Input::old('bill_vat') == '0' ? 'selected' : '' }}>0 %</option>
                    </select>
                </td>
                @include('nimo.domains.car_balance.Main._form_field', array('field'=>'bill_transport_fee_1', 'value'=>$bill_transport_fee_1))
                @include('nimo.domains.car_balance.Main._form_field', array('field'=>'bill_commission_1', 'value'=>$commission_1))
                <input name="active_tab_1" type="hidden" id="active_tab_1" value="{{ ( $step1_data['customer_id'] == 'BH' or $step1_data['customer_id'] == 'ME' ) ? '1' : '0' }}">
            </tr>
        </table>
    </div>

    <div class="tab-pane {{ $step1_data['customer_id'] == 'CH' ? 'active' : '' }}" id="bill_type_2">
        <table class="table table-bordered" id="billDetailed">
            <thead style="background-color: #f0f0f0;">
            <th title="Price">Price</th>
            <th title="VAT (%)">VAT (%)</th>
            <th title="Auction commission">落札料</th>
            <th title="Transport">Transport</th>
            <th title="Vanning">Vanning</th>
            <th title="Commission">Commission</th>
            </thead>
            <tr>
                <td>
                    <input class="input-small" placeholder="Bill Price" title="Bill Price" name="bill_price_2" type="text" id="bill_price_2">
                </td>
                <td>
                    <select name="bill_vat_2" id="bill_vat_2" class="input-small">
                        <option value="8" {{ Input::old('bill_vat') == '8' ? 'selected' : '' }}>8 %</option>
                        <option value="5" {{ Input::old('bill_vat') == '5' ? 'selected' : '' }}>5 %</option>
                        <option value="3" {{ Input::old('bill_vat') == '3' ? 'selected' : '' }}>3 %</option>
                        <option value="0" {{ Input::old('bill_vat') == '0' ? 'selected' : '' }}>0 %</option>
                    </select>
                </td>
                @include('nimo.domains.car_balance.Main._form_field', array('field'=>'bill_rakusatu_2'))
                @include('nimo.domains.car_balance.Main._form_field', array('field'=>'bill_transport_fee_2', 'value'=>$bill_transport_fee_2))
                @include('nimo.domains.car_balance.Main._form_field', array('field'=>'bill_vanning_2'))
                @include('nimo.domains.car_balance.Main._form_field', array('field'=>'bill_commission_2', 'value'=>$commission_2))
                <input name="active_tab_2" type="hidden" id="active_tab_2" value="{{ $step1_data['customer_id'] == 'CH'? '1' : '0' }}">
            </tr>
        </table>
    </div>
    <div class="tab-pane {{ $step1_data['customer_id'] == 'KG' ? 'active' : '' }}" id="bill_type_3">
        <table class="table table-bordered" id="billDetailed">
            <thead style="background-color: #f0f0f0;">
            <th title="Price">Price</th>
            <th title="Commission">Commission</th>
            </thead>
            <tr>
                <td>
                    <input class="input-small" placeholder="Bill Price" title="Bill Price" name="bill_price_3" type="text" id="bill_price_3">
                </td>
                @include('nimo.domains.car_balance.Main._form_field', array('field'=>'bill_commission_3', 'value'=>$commission_3))
                <input name="active_tab_3" type="hidden" id="active_tab_3" value="{{ $step1_data['customer_id'] == 'KG'? '1' : '0' }}">
            </tr>
        </table>
    </div>
    <div class="tab-pane {{ $step1_data['customer_id'] == 'NMC' ? 'active' : '' }}" id="bill_type_4">
        <table class="table table-bordered" id="billDetailed">
            <thead style="background-color: #f0f0f0;">
            <th title="Price">Price</th>
            <th title="VAT (%)">VAT (%)</th>
            <th title="Recycle">Recycle</th>
            <th title="Auction commission">落札料</th>
            <th title="Transport">Transport</th>
            <th title="Vanning">Vanning</th>
            <th title="Others">Others</th>
            <th title="Commission">Commission</th>
            </thead>
            <tr>
                <td>
                    <input class="input-small" placeholder="Bill Price" title="Bill Price" name="bill_price_4" type="text" id="bill_price_4">
                </td>
                <td>
                    <select name="bill_vat_4" id="bill_vat_4" class="input-small">
                        <option value="8" {{ Input::old('bill_vat') == '8' ? 'selected' : '' }}>8 %</option>
                        <option value="5" {{ Input::old('bill_vat') == '5' ? 'selected' : '' }}>5 %</option>
                        <option value="3" {{ Input::old('bill_vat') == '3' ? 'selected' : '' }}>3 %</option>
                        <option value="0" {{ Input::old('bill_vat') == '0' ? 'selected' : '' }}>0 %</option>
                    </select>
                </td>
                @include('nimo.domains.car_balance.Main._form_field', array('field'=>'bill_recycle_4'))
                @include('nimo.domains.car_balance.Main._form_field', array('field'=>'bill_rakusatu_4'))
                @include('nimo.domains.car_balance.Main._form_field', array('field'=>'bill_transport_fee_4', 'value'=>$bill_transport_fee_4))
                @include('nimo.domains.car_balance.Main._form_field', array('field'=>'bill_vanning_4'))
                @include('nimo.domains.car_balance.Main._form_field', array('field'=>'bill_others_4'))
                @include('nimo.domains.car_balance.Main._form_field', array('field'=>'bill_commission_4'))
                <input name="active_tab_4" type="hidden" id="active_tab_4" value="{{ $step1_data['customer_id'] == 'NMC'? '1' : '0' }}">
            </tr>
        </table>
    </div>
    <div class="tab-pane {{ $step1_data['customer_id'] ? '' : 'active' }} {{ $step1_data['customer_id'] == 'BAY' ? 'active' : '' }}" id="bill_type_0">
        <table class="table table-bordered" id="billDetailed">
            <thead style="background-color: #f0f0f0;">
            <th title="Price">Price</th>
            <th title="VAT (%)">VAT (%)</th>
            <th title="Recycle">Recycle</th>
            <th title="Auction commission">落札料</th>
            <th title="Transport">Transport</th>
            <th title="Vanning">Vanning</th>
            <th title="Others">Others</th>
            <th title="Commission">Commission</th>
            </thead>
            <tr>
                <td>
                    <input class="input-small" placeholder="Bill Price" title="Bill Price" name="bill_price_0" type="text" id="bill_price_0">
                </td>
                <td>
                    <select name="bill_vat_0" id="bill_vat_0" class="input-small">
                        <option value="8" {{ Input::old('bill_vat') == '8' ? 'selected' : '' }}>8 %</option>
                        <option value="5" {{ Input::old('bill_vat') == '5' ? 'selected' : '' }}>5 %</option>
                        <option value="3" {{ Input::old('bill_vat') == '3' ? 'selected' : '' }}>3 %</option>
                        <option value="0" {{ Input::old('bill_vat') == '0' ? 'selected' : '' }}>0 %</option>
                    </select>
                </td>
                @include('nimo.domains.car_balance.Main._form_field', array('field'=>'bill_recycle_0'))
                @include('nimo.domains.car_balance.Main._form_field', array('field'=>'bill_rakusatu_0'))
                @include('nimo.domains.car_balance.Main._form_field', array('field'=>'bill_transport_fee_0'))
                @include('nimo.domains.car_balance.Main._form_field', array('field'=>'bill_vanning_0'))
                @include('nimo.domains.car_balance.Main._form_field', array('field'=>'bill_others_0'))
                @include('nimo.domains.car_balance.Main._form_field', array('field'=>'bill_commission_0'))
                <input name="active_tab_0" type="hidden" id="active_tab_0" value="{{ ($step1_data['customer_id'] == 'BAY' or (is_null($step1_data['customer_id']))) ? '1' : '0' }}">
            </tr>
        </table>
    </div>
</div>
</div>
<!--<h4>Bill</h4>
<label for="bill_lump_flag"><input type="checkbox" name="bill_lump_flag" {{ Input::old('bill_lump_flag') ? 'checked' : '' }} id="bill_lump_flag"/>Bill -ийн багцалсан үнийн дүн оруулах</label>
<table class="table table-bordered" id="billDetailed">
    <thead style="background-color: #f0f0f0;">
    <th title="Price">Price</th>
    <th title="VAT">VAT</th>
    <th title="Recycle">Recycle</th>
    <th title="Rakusatu">rakusatu</th>
    <th title="Transport">Transport</th>
    <th title="Vanning">Vanning</th>
    <th title="Others">Others</th>
    <th title="Commission">Commission</th>
    </thead>

    <tr>
        @include('nimo.domains.car_balance.Main._form_field', array('field'=>'bill_price'))
        @include('nimo.domains.car_balance.Main._form_field', array('field'=>'bill_vat'))
        @include('nimo.domains.car_balance.Main._form_field', array('field'=>'bill_recycle'))
        @include('nimo.domains.car_balance.Main._form_field', array('field'=>'bill_rakusatu'))
        @include('nimo.domains.car_balance.Main._form_field', array('field'=>'bill_transport_fee'))
        @include('nimo.domains.car_balance.Main._form_field', array('field'=>'bill_vanning'))
        @include('nimo.domains.car_balance.Main._form_field', array('field'=>'bill_others'))
        @include('nimo.domains.car_balance.Main._form_field', array('field'=>'bill_commission'))
    </tr>
</table>
<table class="table table-bordered" id="billLump">
    <thead style="background-color: #f0f0f0;">
    <th title="Total amount">Total amount</th>
    </thead>

    <tr>
        @include('nimo.domains.car_balance.Main._form_field', array('field'=>'bill_total_amount'))
    </tr>
</table>-->

<button type="submit" class="btn btn-large btn-success"><i class="icon-plus icon-white"></i> Бүртгэх</button> &nbsp;&nbsp;

{{ Form::close() }}
@stop


@section('pagestyles')
<link rel="stylesheet" type="text/css" href={{ asset('assets/bootstrap/bootstrap.css') }} />
<link rel="stylesheet" type="text/css" href={{ asset('assets/bootstrap-fileupload/fileupload.css') }} />
<link rel="stylesheet" type="text/css" href={{ asset('assets/bootstrap-datepicker/datepicker.css') }} />
@stop

@section('pagescripts')
<script type="text/javascript" src={{ asset('assets/bootstrap/bootstrap.js') }}></script>
<script type="text/javascript" src={{ asset('assets/bootstrap-fileupload/fileupload.js') }}></script>
<script type="text/javascript" src={{ asset('assets/bootstrap-datepicker/datepicker.js') }}></script>
<script type="text/javascript" >
    $(document).ready(function(){
        if($('input[name=purchase_date]').val() == '') {
            var d = new Date();

            var month = d.getMonth()+1;
            var day = d.getDate();

            var output = d.getFullYear() + '-' +
                (month<10 ? '0' : '') + month + '-' +
                (day<10 ? '0' : '') + day;

            $('input[name=purchase_date]').val(output);
        }
        $('#bill_lump_flag').on('change', function(e){
            if(this.checked) {
                $('#billDetailed').hide();
                $('#billLump').show();
            } else {
                $('#billDetailed').show();
                $('#billLump').hide();
            }
        });

        if($('#bill_lump_flag').attr('checked')) {
            $('#billDetailed').hide();
            $('#billLump').show();
        } else {
            $('#billDetailed').show();
            $('#billLump').hide();
        }

        $('select[name=car_maker]').on('change', function(e){
            $.get('{{ route('car_balance.car.getCarName') }}?maker='+this.value, function(d){
                $('#car_name').html(d);
            })
            console.log(this.value);
        });

        $(function() {
            $( ".datepicker" ).datepicker({
                defaultDate: "+0d",
                format: "yyyy-mm-dd",
                autoclose: true
            });
        });

    });
    $("#purchase_price").each(function(i){
        $(this).change(function () {
            document.getElementById("bill_price_1").value=document.getElementById("purchase_price").value;
            document.getElementById("bill_price_2").value=document.getElementById("purchase_price").value;
            document.getElementById("bill_price_3").value=document.getElementById("purchase_price").value;
            document.getElementById("bill_price_4").value=document.getElementById("purchase_price").value;
            document.getElementById("bill_price_0").value=document.getElementById("purchase_price").value;
        });
    });
    $("#vat_percent").each(function(i){
        $(this).change(function () {
            document.getElementById("bill_vat_1").value=document.getElementById("vat_percent").value;
            document.getElementById("bill_vat_2").value=document.getElementById("vat_percent").value;
            document.getElementById("bill_vat_4").value=document.getElementById("vat_percent").value;
            document.getElementById("bill_vat_0").value=document.getElementById("vat_percent").value;
        });
    });

$(function() {
    var activeTab = null;
    $('a[data-toggle="tab"]').on('shown', function (e) {
        //e.preventDefault()
//        /$(this).tab('show');
        activeTab = e.target.href;
        bill_type = activeTab.substring(activeTab.length-1);
        setActiveTab(bill_type);
    })
    function setActiveTab(bill_type) {
        for ( var i = 0; i < 5; i++ ) {
            if (i == bill_type) {
                document.getElementById("active_tab_" + bill_type).value = 1;
            } else {
                document.getElementById("active_tab_" + i).value = 0;
            }
        }
    }
});
</script>
@stop